#!/bin/bash
# a script to check the size of a list of files
# you need to provide a list of the files, and it 
# calculate the total size
# Lailin, July. 2012, Ann Arbor

list=$1
tot_size=0

for dir in `less $list`; do
  if [ -d $dir ]; then
    size=`du -sh $dir`
    echo $size >> check_size.$list
    echo $size 
    fsize=`echo $size | cut -f1 -d' '`
    if [[ "$fsize" == *T* ]]; then 
      new_size=`echo $fsize | cut -f1 -d'T'`
      echo "$tot_size, $new_size"
      tot_size=`echo "scale=1; $tot_size*1.0 + $new_size*1000.0" | bc` 
    elif [[ "$fsize" == *G* ]]; then 
      new_size=`echo $fsize | cut -f1 -d'G'`
      echo "$tot_size, $new_size"
      tot_size=`echo "scale=1; $tot_size*1.0 + $new_size*1.0" | bc` 
    elif [[ "$fsize" == *M* ]]; then  
      new_size=`echo $fsize | cut -f1 -d'M'`
      echo "$tot_size, $new_size"
      tot_size=`echo "scale=1; $tot_size*1.0 + $new_size*1.0/1000.0" | bc` 
    else
      new_size=$fsize
      echo "$tot_size, $new_size"
      tot_size=`echo "scale=1; $tot_size*1.0 + $new_size*1.0/1000000.0" | bc` 
    fi
  fi
done

echo "Total size: $tot_size GB" >> check_size.$list

